package com.ktjiaoyu;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisPoolUtils {
    private static String URL="127.0.0.1";
    private static int PORT=6379;
    private static int MAX_IDLE=8;
    private static int MIN_IDLE=8;
    private static int MAX_TOTAL=8;
    private static int MAX_WAIT=10000;

    private static JedisPool pool=null;

    static {
        //获得池子对象
        JedisPoolConfig poolConfig=new JedisPoolConfig();
        //最大闲置个数
        poolConfig.setMaxIdle(MAX_IDLE);
        //最小闲置个数
        poolConfig.setMinIdle(MIN_IDLE);
        //最大链接
        poolConfig.setMaxTotal(MAX_TOTAL);
        //最大等待时间
        poolConfig.setMaxWaitMillis(MAX_WAIT);

        pool=new JedisPool(poolConfig,URL,PORT);
    }
    //获得jedis资源的方法
    public static Jedis getJedis(){
        return pool.getResource();
    }

    //关闭链接
    public static void closeJedis(Jedis jedis){
        if (jedis!=null){
            jedis.close();
        }
    }
}
